//
//  NumberJumpView.m
//  NumberJumpDemo
//
//  Created by sujiang on 16/3/1.
//  Copyright © 2016年 sujiang. All rights reserved.
//

#import "NumberJumpView.h"

#define kFont [UIScreen mainScreen].bounds.size.width >= 375 ? 16 : 14

@interface NumberJumpView ()
{
    CGFloat height;
    CGFloat width;
}

@property (nonatomic, strong) NSMutableArray *lables;

@end

@implementation NumberJumpView

- (NSMutableArray *)lables
{
    if (!_lables) {
        _lables = [NSMutableArray array];
    }
    return _lables;
}

- (instancetype)initWithFrame:(CGRect)frame numberCount:(NSUInteger)count
{
    if (self == [super initWithFrame:frame]) {
        height = frame.size.height;
        width = frame.size.width;
        [self addSubViewsWithNumberCount:count];
    }
    return self;
}

- (void)awakeFromNib
{
    height = 16;
    width = 84;
    [self addSubViewsWithNumberCount:self.numCount];
}

- (void)addSubViewsWithNumberCount:(NSUInteger)count
{
    for (int i = 0; i < count; i++) {
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake((3 + (width - 10) / count) * i, 0, (width - 10) / count, height)];
        [self addSubview:lable];
        lable.layer.cornerRadius = 3;
        lable.layer.masksToBounds = YES;
        lable.font = [UIFont systemFontOfSize:kFont];
        lable.text = @"0";
        lable.backgroundColor = kColors(210, 2, 2, 1);
        lable.textAlignment = NSTextAlignmentCenter;
        lable.textColor = kColors(255, 255, 255, 1);
        lable.tag = 100 + i;
        [self.lables addObject:lable];
    }
    
    UILabel *jiaoLb = (UILabel *)[self viewWithTag:count - 2 + 100];
    UILabel *doit = [[UILabel alloc] initWithFrame:CGRectMake(jiaoLb.right + 1, (jiaoLb.height - 8) / 2, 8, 8)];
    doit.text = @"•";
    doit.font = [UIFont systemFontOfSize:kFont];
    doit.textAlignment = NSTextAlignmentCenter;
    doit.textColor = kColors(102, 102, 102, 1);
    [self addSubview:doit];
    UILabel *fenLb = (UILabel *)[self viewWithTag:count - 1 + 100];
    fenLb.frame = CGRectMake(doit.right + 1, jiaoLb.top, (width - 10) / count, height);
}

- (void)setNumber:(NSString *)number
{
    _number = number;
   // NSLog(@"%@", number);
    CGFloat price = number.floatValue;
    NSInteger qian = (NSInteger)price / 1000;
    NSInteger bai = ((NSInteger)price % 1000 )/ 100;
    NSInteger shi = (((NSInteger)price % 1000 ) % 100) / 10;
    NSInteger ge = ((((NSInteger)price % 1000 ) % 100) % 10);
    NSInteger jiao = (price - (NSInteger)price) * 10;
    for (int i = 0; i < self.lables.count; i++) {
        UILabel *lable = self.lables[i];
        if (i == 0) {
            [self drawLableText:qian lable:lable];
        } else if(i == 1) {
            [self drawLableText:bai lable:lable];
        } else if (i == 2) {
            [self drawLableText:shi lable:lable];
        } else if (i == 3) {
            [self drawLableText:ge lable:lable];
        } else if (i == 4) {
            [self drawLableText:jiao lable:lable];
        } else {
            return;
        }
    }
}

- (void)drawLableText:(NSInteger)number lable:(UILabel *)lable
{
    if (lable.text.integerValue == number) {
        return;
    }
    lable.text = [NSString stringWithFormat:@"%ld", number];
}

- (void)setFontNum:(CGFloat)fontNum
{
    _fontNum = fontNum;
    for (UILabel *lable in self.lables) {
        lable.font = [UIFont systemFontOfSize:fontNum];
    }
}


@end
